<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="table-container">
          <el-table
            @selection-change="selectionChange"
            :data="tableData.list">
            <slot></slot>
          </el-table>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="24">
        <el-pagination
          class="wd-page"
          background
          :page-sizes="[10, 20, 50]"
          :page-size="100"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  name: 'weTable',
  data() {
    return {
      tableData: {
        pageNum: 1,
        pageSize: 10, // 每页显示条目个数
        total: 0 // 总数
      }
    }
  },
  methods: {
    loadData() {
      const params = Object.assign({
        pageNum: this.tableData.pageNum,
        pageSize: this.tableData.pageSize
      }, this.params)
      this.$http.get(this.url, {params}).then(res => {
        this.tableData = res.data
      })
    },
    handleSizeChange(val) {
      this.tableData.pageSize = val
      this.loadData()
    },
    handleCurrentChange(val) {
      this.tableData.pageNum = val
      this.loadData()
    }
  },
  props: {
    url: {
      type: String,
      required: true
    },
    params: {
      type: Object,
      default: () => {}
    },
    selectionChange: {
      type: Function,
      default: () => {}
    }
  },
  created() {
    this.loadData()
  }
}
</script>

<style lang="sass">
  .container
    padding: 20px
    background-color: #fff
    box-sizing: border-box
  .table-container
    margin: 20px 0
    box-sizing: border-box
  .wd-page
    text-align: center
</style>
